package com.ebupt.migu.music.api.service.impl;

import com.ebupt.migu.music.api.entity.MemberPopResponse;
import com.ebupt.migu.music.api.entity.StrategyInfoBean;
import com.ebupt.migu.music.api.entity.StrategyInfoEntity;
import com.ebupt.migu.music.api.mapper.MemberPopMapper;
import com.ebupt.migu.music.api.service.MemberPopService;
import com.ebupt.migu.music.api.util.CrowdService;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.excelSheet.StringUtil;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.*;

@Slf4j
@Service("memberPopService")
public class MemberPopServiceImpl implements MemberPopService {


    //   缓存中判断用户是否存在  业务层
    @Autowired
    private CrowdService crowdService;
    //   会员弹窗mapper层
    @Autowired
    private MemberPopMapper memberPopMapper;


    /**
     * 会员弹窗登录访问
     * @param user_id
     * @param type
     * @return
     */
     @Override
    public MemberPopResponse vipPopUp(String user_id, String  type) {
         //
         StrategyInfoEntity strategyInfoEntity=null;
         MemberPopResponse memberPopResponse;
         // 1.手机号  2.uid  3.idfa/imei  4.全网
         switch (type){
             case "4":
                  strategyInfoEntity=memberPopMapper.strategyInfo();
                 break;
            default:
                //  查询是否存在 数据为空直接返回数据不存在
                List<StrategyInfoEntity> list=memberPopMapper.StrategyInfoList();
                if (list!=null && !list.isEmpty()){
                     strategyInfoEntity=crowdService.queryResult(list,user_id,type);
                }
                break;
         }

        return MemberPopresult(strategyInfoEntity,user_id);
    }

    /**
     *  创建策略
     * @param strategyInfo
     * @return
     */
    @Override
    public ResultObject addStrategy(StrategyInfoBean strategyInfo) {
        //  创建不做判断  取值时取进行钟的最新创建时间
        ResultObject res = new ResultObject();
        String strategyId=getStrategyId();
        strategyInfo.setStrategyId(strategyId);
        strategyInfo.setStrategyType(7);
        if (!StringUtil.isNullOrBlank(strategyInfo.getApproverId())){
            strategyInfo.setExecuteStatus(1);
        }
        switch (strategyInfo.getVipTargetCrowd()){
            case 1:
                // 全客户端
                strategyInfo.setBlack(null);
                strategyInfo.setWhite(null);
                strategyInfo.setGroupId(null);
                break ;
            case 2:
                //  如果 人群规模不为空
                if(strategyInfo.getClientUser()<200){
                    //  不用审批   直接通过审批
                    strategyInfo.setExecuteStatus(4);
                }
                break;
        }
        if (StringUtil.isNullOrBlank(strategyInfo.getGroupId())){
            strategyInfo.setGroupName("全部客户端用户");
        }
        int code= memberPopMapper.addStrategy(strategyInfo);
        if (code==1){
            res.setCode("0000");
            res.setMsg("新增会员弹窗策略成功");
            res.setData(strategyId);
        }
        return res;
    }

    @Override
    public ResultObject updateStrategy(StrategyInfoBean strategyInfo) {
        // 修改时
        ResultObject res = new ResultObject();
        //  修改时判断是不是创建人
        if (!StringUtil.isNullOrBlank(strategyInfo.getCreatorId())){
            int code=memberPopMapper.IsAlike(strategyInfo.getCreatorId(),strategyInfo.getStrategyId());
            if (code==0){
                res.setCode("0000");
                res.setMsg("非创建人不可以修改数据！");
              return res;
            }
        }
        if (!StringUtil.isNullOrBlank(strategyInfo.getApproverId())){
            strategyInfo.setExecuteStatus(1);
        }
        switch (strategyInfo.getVipTargetCrowd()){
            case 1:
                // 全客户端
                strategyInfo.setBlack(null);
                strategyInfo.setWhite(null);
                strategyInfo.setGroupId(null);
                break ;
            case 2:
                //  如果 人群规模不为空
                if(strategyInfo.getClientUser()<200){
                    //  不用审批   直接通过审批
                    strategyInfo.setExecuteStatus(4);
                }
                break;
        }
        if (StringUtil.isNullOrBlank(strategyInfo.getGroupId())){
            strategyInfo.setGroupName("全部客户端用户");
        }
        int code= memberPopMapper.updateStrategy(strategyInfo);
        if (code==1){
            res.setCode("0000");
            res.setMsg("修改会员弹窗策略成功");
        }
        return res;
    }


    /**
     *   返回值组成对象
     * @param strategyInfoEntity
     * @param user_id
     * @return
     */
    private MemberPopResponse MemberPopresult(StrategyInfoEntity strategyInfoEntity,String user_id){
         if (strategyInfoEntity==null){
             return new MemberPopResponse("2","未查到数据",user_id,"","");
         }
         if (strategyInfoEntity!=null){
             return new MemberPopResponse("1","正常返回",user_id,strategyInfoEntity.getVipStrategyPhoto(),strategyInfoEntity.getVipStrategyLink());
         }
             return new MemberPopResponse("3","查询异常",user_id,"","");
    }


    /**
     * 生成会员弹窗策略Id
     * @return
     */
    private String  getStrategyId(){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime time = LocalDateTime.now();
        return "VTC".concat(df.format(time));
    }

}
